package com.aerozhonghuan.logisticsmall.modules.mine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.aerozhonghuan.foundation.eventbus.EventBusManager;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.common.event.UserInfoChangedEvent;
import com.aerozhonghuan.logisticsmall.modules.mine.logic.MineWebRequest;
import com.aerozhonghuan.logisticsmall.modules.user.beans.Company;
import com.aerozhonghuan.logisticsmall.modules.user.beans.UserBean;
import com.aerozhonghuan.logisticsmall.modules.user.logic.UserInfoManager;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import zhwidget.aerozhonghuan.com.zhwidgetlibrary.bottom_select_dialog.BottomPickerDialog;
import zhwidget.aerozhonghuan.com.zhwidgetlibrary.bottom_select_dialog.beans.WheelStyle;

/**
 * 描述 个人中心页面
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class UserInfoFragment extends TitlebarFragment {

    private View rootView;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvCompany;
    private Button btnChangeCompany;
    private ProgressDialogIndicator progressDialogIndicator;
    private UserBean userBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_me_userinfo, null);
            initView();
            initData();
        }
        return rootView;
    }

    private void initView() {
        progressDialogIndicator = new ProgressDialogIndicator(getContext());
        tvName = (TextView) rootView.findViewById(R.id.tv_name);
        tvPhone = (TextView) rootView.findViewById(R.id.tv_phone);
        tvCompany = (TextView) rootView.findViewById(R.id.tv_company);
        btnChangeCompany = (Button) rootView.findViewById(R.id.btn_change_company);
        btnChangeCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MineWebRequest.requestCompanys(getActivity(), progressDialogIndicator, new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                        try {
                            JSONObject jsonObject = new JSONObject(commonMessage.data);
                            JSONArray jsonArray = jsonObject.getJSONArray("datalist");
                            Gson gson = new Gson();
                            Type beanType = new TypeToken<List<Company>>() {
                            }.getType();
                            List<Company> list = gson.fromJson(jsonArray.toString(), beanType);
                            if (list != null && list.size() > 0) {
                                showSwitchDialog(list);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }

    private void showSwitchDialog(final List<Company> list) {
        final List<String> companyList = new ArrayList<>();
        for (Company company : list) {
            companyList.add(company.getName());
        }
        BottomPickerDialog dialog = new BottomPickerDialog(getContext(), companyList);
        dialog.setOnMakeSureClickListener(new BottomPickerDialog.OnMakeSureClickListener() {
            @Override
            public void onSureClick(final int position) {
                if (userBean != null && userBean.getCompany() != null && TextUtils.equals(companyList.get(position), userBean.getCompany().getName())) {
                    return;
                }
                MineWebRequest.changeCompany(getContext(), String.valueOf(list.get(position).getId()), progressDialogIndicator, new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                        tvCompany.setText(companyList.get(position));
                        EventBusManager.post(new UserInfoChangedEvent());
                    }
                });
            }
        });
        WheelStyle wheelStyle = new WheelStyle();
        wheelStyle.titleButtonText = "请选择公司";
        wheelStyle.titleButtonColor = getResources().getColor(R.color.important_text_color);
        wheelStyle.sureButtonColor = getResources().getColor(R.color.colorPrimary);
        dialog.setStyle(wheelStyle);
        dialog.showDialog();
    }

    private void initData() {
        userBean = UserInfoManager.getCurrentUserDetailInfo();
        if (userBean != null) {
            tvName.setText(TextUtils.isEmpty(userBean.getNickname()) ? "未设置" : userBean.getNickname());
            tvPhone.setText(TextUtils.isEmpty(userBean.getMobile()) ? "未设置" : userBean.getMobile());
            if (userBean.getCompany() != null) {
                tvCompany.setText(TextUtils.isEmpty(userBean.getCompany().getName()) ? "未设置" : userBean.getCompany().getName());
            }
        }
    }
}
